package com.jingyanzi.privilege.client;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.jingyanzi.finance.result.ResultSupport;
import com.jingyanzi.privilege.domain.OrgDO;
import com.jingyanzi.privilege.domain.result.ResultDO;
import com.jingyanzi.privilege.query.OrgQueryDO;
import com.jingyanzi.privilege.service.IOrgService;

@RestController
@RequestMapping("/api")
public class DepartmentClientImpl implements DepartmentClientAPI {
	
	@Autowired
	IOrgService orgService;

	@Override
	@GetMapping("/department/{id}")
	public OrgDO get(@PathVariable(value = "id")  Integer id) {
		ResultDO result = orgService.get(id);
		return (OrgDO) result.getModel(ResultSupport.FIRST_MODEL_KEY);
	}
	

	@Override
	@GetMapping("/departments/{name}/{code}/{type}/{orgId}")
	public List<OrgDO> query(@PathVariable(value = "name") String name, 
			@PathVariable(value = "code") String code, 
			@PathVariable(value = "type") Integer type, 
			@PathVariable(value = "orgId") Integer orgId) {
		OrgQueryDO query = new OrgQueryDO();
		if(name.equals("all")) {
			query.setNamecode(null);
		} else {
			query.setNamecode(name);
		}
		if(code.equals("all")) {
			query.setCode(null);
		} else {
			query.setCode(code);
		}
		query.setType(type);
		query.setOrgId(orgId);
		ResultDO result = orgService.query(query);
		return (List<OrgDO>) result.getModel(ResultSupport.FIRST_MODEL_KEY);
	}

}
